import asyncio

from sqlalchemy import select
from sqlalchemy.orm import selectinload

from app.models.system import SysRole, SysUser
from tests.database import async_db_session


async def main():
    try:
        user_id = 3
        async with async_db_session() as session:
            stmt = (
                select(SysUser)
                .where(SysUser.id == user_id)
                .options(selectinload(SysUser.roles).selectinload(SysRole.menus), selectinload(SysUser.dept))
            )
            result = await session.execute(stmt)
            user = result.scalar_one_or_none()
        if not user:
            print(f"User with ID {user_id} not found")
        else:
            print(user.model_dump_json(indent=4))
            print(f"{'*' * 50}")
            print("部门信息：")
            print(user.dept.model_dump_json(indent=4))
            print(f"{'*' * 50}")
            for role in user.roles:
                print(role.model_dump_json(indent=4))
                print(f"{'=' * 50}")
                for menu in role.menus:
                    print(menu.model_dump_json(indent=4))
    except Exception as e:
        print(f"操作失败: {e}")


if __name__ == "__main__":
    asyncio.run(main())
